package com.lvy.hczn.transmit.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lvy.hczn.front.common.annotation.Log;
import com.lvy.hczn.front.common.constant.Constants;
import com.lvy.hczn.front.common.core.controller.BaseController;
import com.lvy.hczn.front.common.core.domain.AjaxResult;
import com.lvy.hczn.front.common.core.domain.entity.SysRole;
import com.lvy.hczn.front.common.core.domain.entity.SysUser;
import com.lvy.hczn.front.common.core.page.TableDataInfo;
import com.lvy.hczn.front.common.enums.BusinessType;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.common.utils.poi.ExcelUtil;
import com.lvy.hczn.front.system.domain.business.GoodsBoxSpec;
import com.lvy.hczn.front.system.domain.system.SupplierBoxPrefix;
import com.lvy.hczn.front.system.domain.transmit.SupplierBoxBatchNumber;
import com.lvy.hczn.front.system.service.GoodsBoxSpecService;
import com.lvy.hczn.front.system.service.SupplierBoxBatchNumberService;
import com.lvy.hczn.front.system.domain.transmit.dto.SupplierBoxBatchNumberExport;
import com.lvy.hczn.front.system.service.SupplierBoxPrefixService;
import com.lvy.hczn.transmit.service.GoodsBoxService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 供应商批次编号管理，供应商生成货箱时的批次和编号，下一批次和编号在记录上累加后更新Controller
 *
 * @author ruoyi
 * @date 2023-05-17
 */
@Controller
@RequestMapping("/system/number")
public class SupplierBoxBatchNumberController extends BaseController {

    private String prefix = "system/number";

    @Autowired
    private SupplierBoxBatchNumberService supplierBoxBatchNumberService;

    @Autowired
    private SupplierBoxPrefixService supplierBoxPrefixService;

    @Autowired
    private GoodsBoxSpecService goodsBoxSpecService;

    @Autowired
    private GoodsBoxService goodsBoxService;

    @RequiresPermissions("system:number:view")
    @GetMapping()
    public String number() {
        return prefix + "/number";
    }

    /**
     * 查询供应商批次编号管理，供应商生成货箱时的批次和编号，下一批次和编号在记录上累加后更新列表
     */
    @RequiresPermissions("system:number:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SupplierBoxBatchNumber supplierBoxBatchNumber) {
        startPage();
        SysUser sysUser = getSysUser();
        if (!sysUser.isAdmin()) {
            supplierBoxBatchNumber.setSupplierCode(String.valueOf(sysUser.getUserId()));
        }
        List<SupplierBoxBatchNumber> list = supplierBoxBatchNumberService.selectSupplierBoxBatchNumberList(supplierBoxBatchNumber);
        return getDataTable(list);
    }

    /**
     * 导出供应商批次编号管理，供应商生成货箱时的批次和编号，下一批次和编号在记录上累加后更新列表
     */
    @RequiresPermissions("system:number:export")
    @Log(title = "供应商批次编号管理，供应商生成货箱时的批次和编号，下一批次和编号在记录上累加后更新", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SupplierBoxBatchNumber supplierBoxBatchNumber) {
        List<SupplierBoxBatchNumber> list = supplierBoxBatchNumberService.selectSupplierBoxBatchNumberList(supplierBoxBatchNumber);
        ExcelUtil<SupplierBoxBatchNumber> util = new ExcelUtil<SupplierBoxBatchNumber>(SupplierBoxBatchNumber.class);
        return util.exportExcel(list, "供应商批次编号管理");
    }

    /**
     * 修改用户
     */
    @GetMapping("/gen")
    public String gen(ModelMap mmap) {
        SysUser sysUser = getSysUser();
        if (sysUser.isAdmin()) {
            List<SupplierBoxPrefix> list = supplierBoxPrefixService.list(Wrappers.<SupplierBoxPrefix>lambdaQuery().eq(SupplierBoxPrefix::getOccupy, Constants.OCCUPY_TRUE).isNotNull(SupplierBoxPrefix::getSupplierCode));
            mmap.put("list", list);
        } else {
            List<SupplierBoxPrefix> list = supplierBoxPrefixService.list(Wrappers.<SupplierBoxPrefix>lambdaQuery().eq(SupplierBoxPrefix::getOccupy, Constants.OCCUPY_TRUE).eq(SupplierBoxPrefix::getSupplierCode, sysUser.getUserId()));
            mmap.put("list", list);
        }
        List<GoodsBoxSpec> specList = goodsBoxSpecService.list(Wrappers.<GoodsBoxSpec>lambdaQuery().eq(GoodsBoxSpec::getIsDelete, Constants.IS_DELETE_FALSE));
        mmap.put("specList", specList);
        return prefix + "/gen";
    }

    /**
     * 生成供应商批次编号管理，供应商生成货箱时的批次和编号，下一批次和编号在记录上累加后更新列表
     */
    @RequiresPermissions("system:number:export")
    @Log(title = "生成商批次编号管理，供应商生成货箱时的批次和编号，下一批次和编号在记录上累加后更新", businessType = BusinessType.EXPORT)
    @PostMapping("/generateBoxNumber")
    @ResponseBody
    public AjaxResult generateBoxNumber(SupplierBoxBatchNumber supplierBoxBatchNumber) {
        SysUser sysUser = getSysUser();
        //懒得加字段，先用remark字段代替存储货箱生产商绑定的仓库
        if(StrUtil.isEmpty(sysUser.getRemark())){
            throw new UtilException("用户未绑定仓库，请先绑定仓库");
        }
        supplierBoxBatchNumber.setSupplierCode(String.valueOf(sysUser.getUserId()));
        supplierBoxBatchNumber.setSupplierName(sysUser.getUserName());
        List<SupplierBoxBatchNumberExport> list = supplierBoxBatchNumberService.generateBoxNumber(supplierBoxBatchNumber);
        goodsBoxService.initGoodsBox(list,supplierBoxBatchNumber,sysUser);
        ExcelUtil<SupplierBoxBatchNumberExport> util = new ExcelUtil<SupplierBoxBatchNumberExport>(SupplierBoxBatchNumberExport.class);
        return util.exportExcel(list, "货箱编号列表");
    }

    /**
     * 新增供应商批次编号管理，供应商生成货箱时的批次和编号，下一批次和编号在记录上累加后更新
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存供应商批次编号管理，供应商生成货箱时的批次和编号，下一批次和编号在记录上累加后更新
     */
    @RequiresPermissions("system:number:add")
    @Log(title = "供应商批次编号管理，供应商生成货箱时的批次和编号，下一批次和编号在记录上累加后更新", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SupplierBoxBatchNumber supplierBoxBatchNumber) {
        return toAjax(supplierBoxBatchNumberService.save(supplierBoxBatchNumber));
    }

    /**
     * 修改供应商批次编号管理，供应商生成货箱时的批次和编号，下一批次和编号在记录上累加后更新
     */
    @RequiresPermissions("system:number:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        SupplierBoxBatchNumber supplierBoxBatchNumber = supplierBoxBatchNumberService.getById(id);
        mmap.put("supplierBoxBatchNumber", supplierBoxBatchNumber);
        return prefix + "/edit";
    }

    /**
     * 修改保存供应商批次编号管理，供应商生成货箱时的批次和编号，下一批次和编号在记录上累加后更新
     */
    @RequiresPermissions("system:number:edit")
    @Log(title = "供应商批次编号管理，供应商生成货箱时的批次和编号，下一批次和编号在记录上累加后更新", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SupplierBoxBatchNumber supplierBoxBatchNumber) {
        return toAjax(supplierBoxBatchNumberService.updateById(supplierBoxBatchNumber));
    }

    /**
     * 删除供应商批次编号管理，供应商生成货箱时的批次和编号，下一批次和编号在记录上累加后更新
     */
    @RequiresPermissions("system:number:remove")
    @Log(title = "供应商批次编号管理，供应商生成货箱时的批次和编号，下一批次和编号在记录上累加后更新", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(supplierBoxBatchNumberService.deleteSupplierBoxBatchNumberByIds(ids));
    }
}
